package test;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.HTMLWriter;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class PhoneInfoAdd {
    public static void main(String[] args) throws IOException {
            Document doc = null;
        try {
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read(new File("src/main/resources/add.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element root = doc.getRootElement();
        Element brandElement = root.addElement("Brand");
        Element typeElement = brandElement.addElement("Type");

        brandElement.addAttribute("name","三星");
        typeElement.addAttribute("name","S22");

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");          //指定XML编码
        XMLWriter writer = new XMLWriter(new FileWriter("src/main/resources/add.xml"),format);
        writer.write(doc);
        writer.close();
    }
}
